#!/bin/sh
# Script de creación de máquina virtual sobre VirtualBox
ODVER="$1"
if test -z "$ODVER"
then
	echo "ERROR: Version missing"
	exit 1
fi

HERE=`pwd`
NAME="OpenDomo-$ODVER"
FS="$HERE/$NAME.ext2"
VDI="$HERE/$NAME.vdi"
# Interfaz conectado a la red, un dhcp server debería estar presente
HOST_IF="eth0"
ISO_PATH="`pwd`/../opendomo-$ODVER-486.iso"
cd
HOME=`pwd`
VBOX="$HOME/VirtualBox VMs"
cd $HERE

echo -n "Removing old duplicates ...         "
if test -e "$VBOX/$NAME" 
then
	echo "WARNING: File exists!"
	echo "Please remove the old virtual machine if the problem persists"
	VBoxManage unregistervm "$NAME" --delete 
	rm -fr "$VBOX/$NAME/" 
fi
echo "DONE"
sleep 3

echo -n "Creating new VM ...                 "
VBoxManage createvm -name $NAME -ostype Linux26 --register
echo "DONE"

if test -f $VDI; then
	echo "Using $VDI"
else
	./iso2vdi.sh $ISO_PATH $VDI
fi

echo "Second stage..."
sleep 2

VBoxManage modifyvm $NAME --memory 512 --usb on --chipset piix3 
VBoxManage modifyvm $NAME --audio pulse --audiocontroller ac97
VBoxManage modifyvm $NAME --nic1 bridged --nictype1 Am79C970A  --bridgeadapter1 eth0
VBoxManage storagectl $NAME --name SATA --add sata --controller IntelAhci --bootable on
VBoxManage storageattach $NAME  --storagectl SATA --port 0 --device 0 --type hdd --medium $VDI
rm -fr ../opendomo-$ODVER-486.ova
VBoxManage export $NAME --output ../opendomo-$ODVER-486.ova
